'use strict';
exports.main = async (event, context) => {
  const db = uniCloud.database()
  
  try {
    // 获取当前用户ID
    const { OPENID, UNIONID, APPID } = context
    if (!OPENID) {
      return {
        code: 403,
        message: '未登录'
      }
    }
    
    const { key, value } = event
    if (!key) {
      return {
        code: 400,
        message: '缺少设置项'
      }
    }
    
    // 更新用户设置
    const result = await db.collection('user_settings')
      .where({
        user_id: OPENID
      })
      .update({
        [key]: value,
        updateTime: Date.now()
      })
    
    return {
      code: 200,
      message: '更新成功',
      data: result
    }
    
  } catch (error) {
    console.error('更新设置失败:', error)
    return {
      code: 500,
      message: error.message || '更新失败'
    }
  }
} 